package com.elsewhat.reddittv.hd.fragments;

import android.app.ListFragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.elsewhat.reddittv.hd.R;

public class SubredditListFragment extends ListFragment{
	OnSubredditSelectedListener listener;
	ArrayAdapter<String> adapter;
	View headerView;
	View footerView;
	
	public interface OnSubredditSelectedListener {
		void onSubredditSelected(String subreddit);
	}
	
	public SubredditListFragment(OnSubredditSelectedListener listener){
		this.listener=listener;

	}
	

	/* Need to inflate header view here
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		headerView = inflater.inflate(R.layout.simple_list_header, null);
		//footerView = inflater.inflate(R.layout.sapmentors_list_header, null);
		
        return super.onCreateView(inflater, container, savedInstanceState);
	}
	
	/* (non-Javadoc)
	 * @see android.app.ListFragment#onActivityCreated(android.os.Bundle)
	 */
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		 
		TextView tvTitle = (TextView) headerView.findViewById(R.id.header_title);
		tvTitle.setText(R.string.reddit_list_header);
		
		if(headerView!=null && getListAdapter()==null){
			getListView().addHeaderView(headerView,null,false);
		}

		String[] subreddits= getActivity().getResources().getStringArray(R.array.subreddits_default);
		adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,subreddits);
		setListAdapter(adapter);
		
		ListView lv = getListView();
        lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        lv.setCacheColorHint(Color.TRANSPARENT);  
	}
	


	/* (non-Javadoc)
	 * @see android.app.ListFragment#onListItemClick(android.widget.ListView, android.view.View, int, long)
	 */
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		if(listener!=null){
			listener.onSubredditSelected((String)getListView().getItemAtPosition(position));
		}
	}

	/* (non-Javadoc)
	 * @see android.app.Fragment#onSaveInstanceState(android.os.Bundle)
	 */
	@Override
	public void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
	}
	


}
